#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


int main()
{
    umask(0);
    int fd = open("log.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //int fd = open("log.txt", O_CREAT | O_WRONLY | O_APPEND, 0666);
    //int fd = open("log.txt", O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return 1;
    }
    printf("fd: %d\n", fd);

    int cnt = 5;
    const char *msg = "hello world\n";
    while(cnt)
    {
        write(fd, msg, strlen(msg)); //fd: 后面讲， msg：缓冲区首地址， len: 本次读取，期望写入多少个字节的数据。 返回值：实际写了多少字节数据
        cnt--;
    }

    close(fd);
    return 0;
}
